package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.innovation.chengzhangriji.R;

public class GuideViewPagerAdapter extends PagerAdapter implements View.OnClickListener {

    private static final int GUIDE_PAGE_LENGTH = 3;

    public interface OnClickGuideViewListener {

        void onClickGuideViewStart();

    }

    private Context mContext;

    private OnClickGuideViewListener mOnClickGuideViewListener;

    public GuideViewPagerAdapter(Context context, OnClickGuideViewListener listener) {
        mContext = context;
        mOnClickGuideViewListener = listener;
    }

    @Override
    public int getCount() {
        return GUIDE_PAGE_LENGTH;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView;
        switch (position) {
            case 0:
                itemView = inflater.inflate(R.layout.layout_guide_1, container, false);
                container.addView(itemView);
                return itemView;
            case 1:
                itemView = inflater.inflate(R.layout.layout_guide_2, container, false);
                container.addView(itemView);
                return itemView;
            case 2:
                itemView = inflater.inflate(R.layout.layout_guide_3, container, false);
                CuteButton button = (CuteButton) itemView.findViewById(R.id.btn_guide_start);
                button.setOnClickListener(this);
                container.addView(itemView);
                return itemView;
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public void onClick(View v) {
        if (mOnClickGuideViewListener == null) {
            return;
        }
        mOnClickGuideViewListener.onClickGuideViewStart();
    }
}
